﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.Drawing.Printing;

namespace BarManager.Printer.Ticket.WCF
{
    public class Process : IContract
    {
        public void PrintOrder(TicketOrder ticketOrder)
        {
            try
            {
                Ticket ticket = new Ticket();
                ticket.MaxChar = 35;
                ticket.MaxCharDescription = 35;

                #region Cabecera
                string ordenLine = "Nro. Orden:" + ticketOrder.OrdenId;
                string fechaLine = "Fecha:" + DateTime.Now.ToString("dd/MM/yyyy");
                string horaLine = "Hora:" + DateTime.Now.ToString("HH:mm:ss");
                string zonaLine = "Zona:" + ticketOrder.Zona;
                string mesaLine = "Mesa:" + ticketOrder.Mesa;
                string mozoLine = "Mozo:" + ticketOrder.Mozo;

                ticket.AddHeaderLine("           JACOVINO S.H.          ");
                ticket.AddHeaderLine("Av.Illia 702 - Catamarca - CP:4700");
                ticket.AddHeaderLine("Tel: 44-63328 / 44-25549");
                ticket.AddHeaderLine(("").PadRight(ticket.MaxChar, '*'));
                ticket.AddHeaderLine("");
                ticket.AddHeaderLine(ordenLine + (" ").PadRight(ticket.MaxChar - (ordenLine.Length + mozoLine.Length)) + mozoLine);
                ticket.AddHeaderLine(zonaLine + (" ").PadRight(ticket.MaxChar - (zonaLine.Length + mesaLine.Length)) + mesaLine);
                ticket.AddHeaderLine(fechaLine + (" ").PadRight(ticket.MaxChar - (fechaLine.Length + horaLine.Length)) + horaLine);
                ticket.AddHeaderLine("");
                #endregion

                #region Detalle

                #region Sin Pagar
                foreach (var item in ticketOrder.Detalle)
                {
                    string cantidadYPrecioLine = item.Cantidad.ToString() + " x " + item.PrecioUnitario.ToString("G");
                    string subtotalLine = item.SubTotal.ToString("G");

                    ticket.AddHeaderLine(item.Descripcion);
                    ticket.AddHeaderLine(cantidadYPrecioLine + (" ").PadRight(ticket.MaxChar - (cantidadYPrecioLine.Length + subtotalLine.Length)) + subtotalLine);
                }

                //- SubTotal            
                if (ticketOrder.TotalRegalado + ticketOrder.TotalPagado > 0)
                {
                    ticket.AddHeaderLine(("").PadRight(ticket.MaxChar, '-'));
                    string subTotalLine = "SubTotal";
                    string montoSubTotalLine = (ticketOrder.TotalPagado + ticketOrder.TotalRegalado + ticketOrder.TotalPorPagar).ToString("G");
                    ticket.AddHeaderLine(subTotalLine + (" ").PadRight(ticket.MaxChar - (subTotalLine.Length + montoSubTotalLine.Length)) + montoSubTotalLine);
                }
                #endregion

                #region Regalos
                if (ticketOrder.TotalRegalado > 0)
                {
                    ticket.AddHeaderLine("");
                    ticket.AddHeaderLine("Regalo");
                    foreach (var item in ticketOrder.Detalle)
                    {
                        if (item.CantidadRegalo > 0)
                        {
                            string subTotalRegalos = (item.SubTotalRegalado * -1).ToString("G");
                            string cantidadYPrecioRegalosLine = item.CantidadRegalo.ToString() + " x " + item.PrecioUnitario.ToString("G");

                            ticket.AddHeaderLine(item.Descripcion);
                            ticket.AddHeaderLine(cantidadYPrecioRegalosLine + (" ").PadRight(ticket.MaxChar - (cantidadYPrecioRegalosLine.Length + subTotalRegalos.Length)) + subTotalRegalos);
                        }
                    }
                }
                #endregion

                #region Pagado
                if (ticketOrder.TotalPagado > 0)
                {
                    ticket.AddHeaderLine("");
                    ticket.AddHeaderLine("Pagado");
                    foreach (var item in ticketOrder.Detalle)
                    {
                        if (item.CantidadPagado > 0)
                        {
                            string subTotalPagado = (item.SubTotalPagado * -1).ToString("G");
                            string cantidadYPrecioPagadaLine = item.CantidadPagado.ToString() + " x " + item.PrecioUnitario.ToString("G");

                            ticket.AddHeaderLine(item.Descripcion);
                            ticket.AddHeaderLine(cantidadYPrecioPagadaLine + (" ").PadRight(ticket.MaxChar - (cantidadYPrecioPagadaLine.Length + subTotalPagado.Length)) + subTotalPagado);
                        }
                    }
                }
                #endregion

                #endregion

                #region Totales
                //- Total           
                ticket.AddHeaderLine(("").PadRight(ticket.MaxChar, '-'));
                string totalLine = "TOTAL";
                string montoTotalLine = (ticketOrder.Total).ToString("G");
                ticket.AddHeaderLine(totalLine + (" ").PadRight(ticket.MaxChar - (totalLine.Length + montoTotalLine.Length)) + montoTotalLine);

                //- Por Pagar
                if (ticketOrder.TotalRegalado + ticketOrder.TotalPagado > 0)
                {
                    string totalPorPagarLine = "Por Pagar";
                    string montoTotalPorPagarLine = (ticketOrder.TotalPorPagar).ToString("G");
                    ticket.AddHeaderLine(totalPorPagarLine + (" ").PadRight(ticket.MaxChar - (totalPorPagarLine.Length + montoTotalPorPagarLine.Length)) + montoTotalPorPagarLine);
                }
                #endregion

                //- Pie 
                ticket.AddFooterLine("");
                ticket.AddFooterLine("Comprobante no válido como factura");
                ticket.AddFooterLine("   ¡Te esperamos nuevamente!");

                //- Imprimir               
                ticket.PrintTicket(ConfigurationManager.AppSettings["DefaultPrinterName"]);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("PrintOrder: " + ex.Source, ex.Message + " - " + ex.StackTrace, EventLogEntryType.Error);
            }
        }      

        public void PrintKitchen(TicketOrder ticketKitchen)
        {
            throw new NotImplementedException();
        }
    }
}
